
//时间选择器
//开始时间
laydate.render({
	elem: '#start_date',
	type: 'datetime',
	/*done:function(value,date){
        console.log(value);
        console.log(date);
    }*/
});
//结束时间
laydate.render({ 
  elem: '#end_date',
  type: 'datetime' 
});

//重置按钮
$('#resetBtn').bind('click',function(){
	$('#key_txt').val('请输入关键字');
	$('#software_txt').val('请输入软件名称');
	$('#process_txt').val('请输入进程名称');
	$('#hostip_select').val('请输入主机名称');
	//start_date
	//$('#start_date').val('请选择开始日期');
	
});

jQuery(function($){
	
		//时间处理格式
		function timeFormat(timeStamp){
			var time = new Date(timeStamp);//改为时间戳
			
			var year = time.getFullYear();
			var month = time.getMonth()+1;
			//month++;//month 一月是0 二月是1
			if(month<10){
				month='0'+month;
			}
			var day = time.getDate();
			if(day<10){
				day='0'+day;
			}
			var hour = time.getHours();
			if(hour<10){
				hour='0'+hour;
			}
			var minutes = time.getMinutes();
			if(minutes<10){
				minutes='0'+minutes;
			}
			var second = time.getSeconds();
			if(second<10){
				second='0'+second;
			}
			var result = year+'-'+month+'-'+day+' '+hour+':'+minutes+':'+second;
			return result;
		}
		//获取当前时间
		var curTime=new Date();
		var end = curTime.getTime(); //改为时间戳
		var start = end - 1*60*1000 * 60 *0.5;
		
		//Date.toDateString( )
		//设置默认查当前48小时 软件名为iotdb的日志信息
		var keys = '';
		var software = '';
		var processname = '';
		var hostip = '';
		var startTime = timeFormat(start);
		var endTime = timeFormat(end);
		//alert(startTime+'...'+endTime+'...');
		//alert(timeFormat(endTime));
		
		function searchLog(){
			$.ajax({
	            type : "post",  
	            cache:false,
	            async:true,
	            url: "/detection/log/list/filter",
	            data:{'keys':keys,
	            	  'software':software,
	            	  'processname':processname,
	            	  'hostip':hostip,
	            	  'startTime':startTime,
	            	  'endTime':endTime
	            },
	            success: function(data){
	            	//alert("操作成功");
	            	if(data.success){
	            		$('#logshow_table').footable();//对footable进行初始化
	            		//console.log(data);
	            		var log_row = '';
	            		for(var i = 0; i < data.data.length; i++){    //获取后台传过来的url中的数据
	            			var id = i+1;
	            			//var id = data.data[i].id;
				            var logtime = data.data[i].logtime;
				            logtime = timeFormat(logtime);
				            //logtime = new Date(logtime).toLocaleString();
				            var hostip = data.data[i].hostip;
				            var software = data.data[i].software;
				            var processname = data.data[i].processname;
				            var faulttype = data.data[i].faulttype;
				            var content = data.data[i].content;
				            var user = data.data[i].user;
				            //var content = data.data[i].content;
				            /*if(software=="mysql-detection-856dd68bcf-tb8nl"){
				                software = "kingbase";
				            }
				            if(processname=="mysql-detection"){
				                processname="kingbase-detection";
				            }*/
				            log_row += '<tr><td>'   + id + '</td><td>' 
										            + logtime + '</td><td>' 
										            + hostip + '</td><td>' 
										            + software + '</td><td>' 
									    	     	+ processname + '</td><td>' 
									    	     	+ user +'</td><td>'
									    	     	/*+ faulttype + '</td><td>' */
									    	     	+ content + '</td></tr>';// '</td><td>' + content +
					   }
		               //console.log(log_row);
	            	   $('#logshow_body').html(log_row);//将每一行的数据加载到html中的id为config_body的tbody中
	            	   $('#logshow_table').footable();
	               }
	               else{
	            	    $('#logshow_table').footable();//对footable进行初始化
	    				var log_search_null = '<tr class="footable-empty"><td colspan="7">没有符合条件的查询结果</td></tr>';
	    				var tabletTbody = $("#logshow_table").find('tbody');
	            	    tabletTbody.empty();
	            	    tabletTbody.append(log_search_null);
	            	    alert("没有符合条件的查询结果");
	               }
	 		      
	 		       //console.log(row);
	            },
	            error: function(){
	            	alert("操作失败!");
/*	            	alert(jqXHR.responseText);
		            alert(jqXHR.status);//500 lab1node_utilization
		            alert(jqXHR.readyState);//4
		            alert(jqXHR.statusText);//error
*/	            }
	        });			
		}
		searchLog();
		
		//搜索按钮
		$('#searchBtn').bind('click',function(){
			
			keys=$('#key_txt').val();
			software=$('#software_txt').val();
			processname=$('#process_txt').val();
			hostip=$('#hostip_select').val();
			startTime=$('#start_date').val();
			endTime=$('#end_date').val();
			//重置占用了其value属性的值 对未设置value进行清空
			if(keys=='请输入关键字'){
				keys='';
			}if(software=='请输入软件名称'){
				software='';
			}if(processname=='请输入进程名称'){
				processname='';
			}if(hostip=='请输入主机名称'){
				hostip='';
			}
			
			/*alert('keys:'+keys +' software:'+ software +' processname:'+processname +' hostip:'
						 + hostip +' startTime:'+startTime +' endTime:'+endTime);
			*/
			if(startTime==''){
				alert("开始日期不能为空");
			}else if(endTime==''){
				alert("结束日期不能为空");
			}else {
				searchLog();
			}
			
		});
		
		
});




	